package com.photoeditor.demo.app;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.common.base.ComponentContext;
import com.common.base.app.BaseActivity;
import com.common.statistics.StatisticsUtils;
import com.photoeditor.demo.model.preference.SpUtils;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.util.AndroidDevice;
import com.photoeditor.demo.util.update.UpdateUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by winn on 17/4/14.
 */

public class XActivity extends BaseActivity {

    protected boolean mIsCanShowRateDialog = true;
    protected UpdateUtil mUpdateUtil;
    private boolean mIsFinishing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isTranslucentStatusBar()
                && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        StatisticsUtils.statisics(StatisticsConstant.UMENG_CUSTOM_EVENT_PREFIX_FUNCTION + "_page", "in",
                "class", getClass().getSimpleName());

        AppApplication.getApplication().setActiveFromBackground(false);
    }

    protected boolean isTranslucentStatusBar() {
        return false;
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (mUpdateUtil == null) {
            mUpdateUtil = new UpdateUtil(this);
        }
        if (AppApplication.getApplication().isActiveFromBackground()) {
            AppApplication.getApplication().setActiveFromBackground(false);
//            if(mIsCanShowRateDialog) {
//                RateGuide.showRate(this);
//            }
            // 用户重新进入应用后才弹升级弹窗
            if (!mUpdateUtil.isUpdateDialogShowing()) {
                mUpdateUtil.checkUpdate();
            }
//            AdPreloadManager.getInstance().preloadAllAd();
        }
        if (!mUpdateUtil.isUpdateDialogShowing() && mUpdateUtil.isNeedForceUpdate()) {
            mUpdateUtil.checkUpdate();
        }
        AppApplication.getApplication().setShowSplashAd(false);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (!AndroidDevice.isAppOnForeground(ComponentContext.getContext())) {
            //app 进入后台
            AppApplication.getApplication().setActiveFromBackground(true);
            SpUtils.obtain().save("appFromBackground", true);
            SpUtils.obtain().save("appFromBackgroundDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        }

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
            AppApplication.getApplication().setShowSplashAd(true);
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onBackPressed() {
        try {
            super.onBackPressed();
        } catch (Exception e) {
            finish();
        }
    }

    @Override
    public void finish() {
        if (mIsFinishing) {
            return;
        }
        mIsFinishing = true;
        StatisticsUtils.statisics(StatisticsConstant.UMENG_CUSTOM_EVENT_PREFIX_FUNCTION + "_page", "out",
                "class", getClass().getSimpleName());
        super.finish();
    }

    @Override
    @SuppressWarnings("RestrictedApi")
    public void startActivityForResult(final Intent intent, final int requestCode, @Nullable final Bundle options) {
        super.startActivityForResult(intent, requestCode, options);
    }

    private String getTargetActivityName(@NonNull Intent intent) {
        ComponentName componentName = intent.getComponent();
        if (componentName != null) {
            String className = componentName.getClassName();
            if (className != null) {
                try {
                    return className.substring(className.lastIndexOf(".") + 1);
                } catch (IndexOutOfBoundsException ignore) {}
            }
        }
        return null;
    }

}
